<%@include file="/WEB-INF/header.jsp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<div id="page-wrapper">

	<!-- 模态框（Modal） -->
	<form action="${pageContext.request.contextPath }/user/addUser.action"
		class="form-horizontal" role="form">
		<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
			aria-labelledby="myModalLabel" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal"
							aria-hidden="true">&times;</button>
						<h4 class="modal-title" id="myModalLabel">添加用户</h4>
					</div>
					<div class="modal-body">
						<!-- 						<div class="form-group">
							<label for="firstname" class="col-sm-2 control-label">用户ID</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="user_id" id=""
									placeholder="请输入用户ID">
							</div>
						</div> -->
						<div class="form-group">
							<label for="firstname" class="col-sm-2 control-label">用户名</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="user_name" id=""
									placeholder="请输入用户名" required="required">
							</div>
						</div>
						<div class="form-group">
							<label for="lastname" class="col-sm-2 control-label">密码</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="user_pass" id=""
									placeholder="请输入密码" required="required">
							</div>
						</div>
						<div class="form-group">
							<label for="firstname" class="col-sm-2 control-label">年龄</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="user_age" id=""
									placeholder="请输入年龄" required="required">
							</div>
						</div>
						<div class="form-group">
							<label for="firstname" class="col-sm-2 control-label">性别</label>
							<div class="col-sm-10">
								<label class="radio-inline"> <input type="radio"
									name="user_sex" id="user_sex_1" value="1" checked> 男
								</label> <label class="radio-inline"> <input type="radio"
									name="user_sex" id="user_sex_0" value="0"> 女
								</label>
							</div>
						</div>
						<div class="form-group">
							<label for="firstname" class="col-sm-2 control-label">Email</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="user_email" id=""
									placeholder="请输入Email" required="required">
							</div>
						</div>
						<div class="form-group">
							<label for="firstname" class="col-sm-2 control-label">权限等级</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="user_rank" id=""
									placeholder="请输入权限等级" required="required">
							</div>
						</div>
						<div class="form-group">
							<label for="firstname" class="col-sm-2 control-label">身份</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="user_identity"
									id="" placeholder="请输入身份" required="required">
							</div>
						</div>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
						<button type="submit" class="btn btn-primary">提交</button>
					</div>
				</div>
				<!-- /.modal-content -->
			</div>
			<!-- /.modal -->
		</div>
	</form>
	<table class="table table-striped">
		<caption>
		<c:if test="${sessionScope.USER_SESSION.user_rank == 1}">
			<!-- 按钮触发模态框 -->
			<div class="col-md-3 data1">
				<button class="btn btn-primary btn-sm" data-toggle="modal"
					data-target="#myModal">添加用户</button>
				<br />
			</div>
			<br />
			<form action="${pageContext.request.contextPath}/user/findAll.action"
				method="post" class="form-horizontal" role="form">
				<div class="col-md-3 data1">
					<input type="text" class="form-control" name="data"
						placeholder="请输入查询内容" value="">
				</div>
				<button type="submit" class="btn bg-maroon">搜索</button>
			</form>
			</c:if>
		</caption>
		<thead>
			<tr>
			<c:if test="${sessionScope.USER_SESSION.user_rank == 1}">
				<th>用户序号</th>
				<th>用户名/学生学号</th>
				<th>密码</th>
				<th>年龄</th>
				<th>邮箱</th>
				<th>身份</th>
				<th>权限等级</th>
				<th>操作</th>
				</c:if>
			</tr>	
		</thead>
		<tbody>
		<div>
		
<%-- 		<c:set value="${len}" var="len"></c:set> --%>
		
		</div>
			<c:forEach items="${userlist}" var="l" varStatus="num">
			<c:if test="${sessionScope.USER_SESSION.user_rank == 1}">
				<tr>
					<td>${num.index+1}</td>
					<td>${l.user_name}</td>
					<td>${l.user_pass}</td>
					<td>${l.user_age}</td>
					<td>${l.user_email}</td>
					<td>${l.user_identity}</td>
					<td>${l.user_rank}</td>
					<td><a href="#" class="btn btn-info btn-xs"
						data-toggle="modal" data-target="#goodEditDialog" role="button"
						onclick="returnInfor(${l.user_id})">修改</a> <a
						href="${pageContext.request.contextPath }/user/delUser.action?userid=${l.user_id}"
						onclick="javascript:return confirm('确实要删除该用户吗?')"
						class="btn btn-danger btn-xs" role="button">删除</a></td>
				</tr>
				</c:if>
			</c:forEach>
			<div class="modal fade" id="goodEditDialog" tabindex="-1"
				role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
				<div class="modal-dialog">
					<div class="modal-content">
						<div class="modal-header">
							<button type="button" class="close" data-dismiss="modal"
								aria-hidden="true">&times;</button>
							<h4 class="modal-title" id="myModalLabel">修改用户</h4>
						</div>
						<div class="modal-body">
							<form id="edit_user_form">
								<div class="form-group" hidden>
									<label for="firstname" class="col-sm-2 control-label">用户ID</label>
									<div class="col-sm-10">
										<input type="text" class="form-control" name="user_id" id="user_id"
											placeholder="请输入用户ID" required="required">
									</div>
								</div>
								<div class="form-group">
									<label for="firstname" class="col-sm-2 control-label">用户名</label>
									<div class="col-sm-10">
										<input type="text" class="form-control" name="user_name"
											id="user_name" placeholder="请输入用户名" required="required">
									</div>
								</div>
								<div class="form-group">
									<label for="firstname" class="col-sm-2 control-label">密码</label>
									<div class="col-sm-10">
										<input type="text" class="form-control" name="user_pass"
											id="user_password" placeholder="请输入密码" required="required">
									</div>
								</div>
								<div class="form-group">
									<label for="firstname" class="col-sm-2 control-label">Email</label>
									<div class="col-sm-10">
										<input type="text" class="form-control" name="user_email"
											id="user_email" placeholder="请输入邮箱" required="required">
									</div>
								</div>
								<div class="form-group">
									<label for="firstname" class="col-sm-2 control-label">权限等级</label>
									<div class="col-sm-10">
										<input type="text" class="form-control" name="user_rank"
											id="user_rank" placeholder="请输入权限等级" required="required">
									</div>
								</div>
						</div>
						<div class="modal-footer">
							<button type="button" class="btn btn-default"
								data-dismiss="modal">关闭</button>
							<button type="submit" class="btn btn-primary"
								onclick="updateUser()">提交</button>
						</div>
					</div>
					<!-- /.modal-content -->
				</div>
				<!-- /.modal -->
			</div>
			</form>
		</tbody>
	</table>
	<c:if test="${sessionScope.USER_SESSION.user_rank == 1}">
	<div style="text-align: center">
		<button onclick="back()">返回</button>
	</div>
	</c:if>
	<script type="text/javascript">
	  function returnInfor(user_id) {
			
			$.post("${pageContext.request.contextPath}/user/getUserId.action",{"user_id":user_id},function(data){
				$("#user_id").val(data.user_id);
				$("#user_name").val(data.user_name);
		         $("#user_password").val(data.user_pass);
		         $("#user_email").val(data.user_email);
		         $("#user_rank").val(data.user_rank);
			});
		}
	  function updateUser() {
	  	
			$.post("${pageContext.request.contextPath}/user/updateUser.action",$("#edit_user_form").serialize(),function(data){
				if(data =="OK"){
					alert("用户信息修改成功！");
					window.location.reload();
				}else{
					alert("用户信息修改失败！");
					window.location.reload();
				}
			});
		}
	  
	  function back(){
		  window.history.go(-1);
	  }
</script>
</div>
<%@include file="/WEB-INF/footer.jsp"%>